/*******************************************************************************
 * Copyright (c) 2010 marvin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * Contributors:
 *     marvin - initial API and implementation
 ******************************************************************************/

package com.marv42.ebt.newnote;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;



class MyGestureListener extends SimpleOnGestureListener
                         implements OnTouchListener
{
   private int mSwipeMinDistance;
   private int mSwipeMaxOffPath;
   private int mSwipeThresholdVelocity;

   private GestureDetector mGestureDetector;



   public MyGestureListener(Context context)
   {
      mGestureDetector = new GestureDetector(context, this);

      final ViewConfiguration vc = ViewConfiguration.get(context);
      mSwipeMinDistance       = vc.getScaledTouchSlop();
      mSwipeMaxOffPath        = mSwipeMinDistance;
      mSwipeThresholdVelocity = vc.getScaledMinimumFlingVelocity();
   }



   @Override
   public boolean
   onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
   {
      if (e1 == null || e2 == null)
         return false;

      if (Math.abs(e1.getY() - e2.getY()) > mSwipeMaxOffPath)
         return false;

      if (Math.abs(e1.getX() - e2.getX()) > mSwipeMinDistance &&
          Math.abs(velocityX) > mSwipeThresholdVelocity)
      {
         Log.d(EbtNewNote.LOG_TARGET, "MyGestureListener::onFling -> true");
         return true;
      }

      return false;
   }



   public boolean
   onTouch(View v, MotionEvent event)
   {
      return mGestureDetector.onTouchEvent(event);
   }



   public GestureDetector
   getDetector()
   {
      return mGestureDetector;
   }
}
